package com.lt.dailytest.file;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class FileTest {
    private static final String FILE_AFTER_NAME = ".mp4";
    private static final String FILE_PREFIX = "E:\\Using\\";

    public static void main(String[] args) {
        List<String> pathList = getStringList();
        String pathJoin = pathList.stream().collect(Collectors.joining(","));
        System.out.println("pathJoin：" + pathJoin);

        getFiles(pathJoin, null, FILE_AFTER_NAME, false);
//        rennameFileName(path3, null);
    }

    /**
     * 获取文件集合地址
     *
     * @return
     */
    private static List<String> getStringList() {
        String path1 = FILE_PREFIX + "2021-01-30";
        String path2 = FILE_PREFIX + "2021-02-27";
        String path3 = FILE_PREFIX + "2021-03-26";
        String path4 = FILE_PREFIX + "2021-05-04";
        String path5 = FILE_PREFIX + "2021-06-20";
        String path6 = FILE_PREFIX + "2021-08-29";
        String path7 = FILE_PREFIX + "2021-10-29";
        String path8 = FILE_PREFIX + "2021-11-10";
        String path9 = FILE_PREFIX + "2021-12-04";
        String path10 = FILE_PREFIX + "2022-01-07";
        String path11 = FILE_PREFIX + "2022-01-27";
        String path12 = FILE_PREFIX + "2022-02-18";
        String path13 = FILE_PREFIX + "2022-03-10";
        String path14 = FILE_PREFIX + "2022-03-21";
        String path15 = FILE_PREFIX + "2022-04-25";
        String path16 = FILE_PREFIX + "2022-05-24";
        String path17 = FILE_PREFIX + "2022-07-08";
        String path18 = FILE_PREFIX + "2022-10-06";
        String path19 = FILE_PREFIX + "2023-02-25";


        List<String> pathList = Arrays.asList(path1, path2, path3, path4, path5, path6, path7, path8,
                path9, path10, path11, path12, path13, path14, path15, path16, path17, path18, path19);
        return pathList;
    }

    /**
     * @param filepath        eg file,file2
     * @param filepathSplite  eg 文件地址分割符 为英文状态下面的逗号
     * @param fileNameReplace eg file,file2
     * @param isDelRepeatFile 是否删除重复文件
     * @return
     */
    public static ArrayList<String> getFiles(String filepath, String filepathSplite,
                                             String fileNameReplace, boolean isDelRepeatFile) {
        ArrayList<String> files = new ArrayList<String>();
        if (StringUtils.isEmpty(filepath)) {
            return null;
        }
        if (StringUtils.isEmpty(filepathSplite)) {
            filepathSplite = ",";
        }
        if (StringUtils.isEmpty(fileNameReplace)) {
            fileNameReplace = FILE_AFTER_NAME;
        }
        StringBuilder builder = new StringBuilder();
        String[] split = filepath.split(filepathSplite);
        HashMap<String, String> hashMap = new HashMap<>();
        for (String filename : split) {
            File file = new File(filename);
            File[] tempLists = file.listFiles();
            for (int i = 0; i < tempLists.length; i++) {
                if (tempLists[i].isFile()) {
                    String name = tempLists[i].getName();
                    if (!name.contains(fileNameReplace)) {
                        builder.append("path:" + tempLists[i].getAbsolutePath() + " \n");
                    }
                    boolean contains = hashMap.keySet().contains(name);
                    if (contains) {
                        System.out.println(name + "=============" + filename);
                        System.out.println(hashMap.get(name) + "======before=======");
                        if (isDelRepeatFile) {
                            tempLists[i].delete();
                        }
                    }
                    hashMap.put(name, filename);

                }
            }
        }
        System.out.println("size : " + hashMap.keySet().size() + " 个文件");
        System.out.println("不以" + FILE_AFTER_NAME + "结尾的文件：" + builder);
        return files;
    }

    /**
     * 更换文件名称
     *
     * @param path           文件地址
     * @param fileNameSplite 文件分隔符默认为 _
     */
    public static void rennameFileName(String path, String fileNameSplite) {
        if (StringUtils.isEmpty(fileNameSplite)) {
            fileNameSplite = "_";
        }

        String[] split = path.split(",");
        for (String filePath : split) {
            File folder = new File(filePath);
            File[] files = folder.listFiles();
            for (File file : files) {
                String name = file.getName();
                String[] s = name.split(fileNameSplite);
                if (name.contains(FILE_AFTER_NAME)) {
                    continue;
                }
                File newfile = new File(path + "/" + s[0] + FILE_AFTER_NAME);
                System.out.println(newfile);
                file.renameTo(newfile);
            }
        }

    }
}
